﻿using DataManagementBoard.Model;
using DataManagementBoard.Model.Provider;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Ioc;
using GalaSoft.MvvmLight.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataManagementBoard.ViewModel
{
    public class StopTypeViewModel:ViewModelBase
    {
        private List<StopType> stopTypes;
        private StopTypeProvider stopTypeProvider = new StopTypeProvider();
        public StopTypeViewModel()
        {
            stopTypes = stopTypeProvider.Select();
        }

        public List<StopType> StopTypes
        {
            get { return stopTypes; }
            set { stopTypes = value; RaisePropertyChanged(); }
        }

        public RelayCommand OpenAddStopTypeWindowCommand
        {
            get
            {
                return new RelayCommand(() => {
                    var dia = SimpleIoc.Default.GetInstance<IDialogService>();
                    dia.ShowMessage("AddStopTypeWindow", "提示");
                    StopTypes = stopTypeProvider.Select();
                });
            }
        }

        public RelayCommand<StopType> OpenEditStopTypeWindowCommand
        {
            get
            {
                return new RelayCommand<StopType>((model) =>
                {
                    var vm = SimpleIoc.Default.GetInstance<EditStopTypeWindowViewModel>();
                    if (vm == null) return;
                    //vm.Line = line;
                    var dia = SimpleIoc.Default.GetInstance<IDialogService>();
                    dia.ShowMessage("EditStopTypeWindow", "提示");
                    StopTypes = stopTypeProvider.Select();
                });
            }
        }

        public RelayCommand<StopType> DeleteLineWindowCommand
        {
            get
            {
                return new RelayCommand<StopType>((model) => {
                    if (model == null) return;
                    var dialog = SimpleIoc.Default.GetInstance<IDialogService>();
                    var task = dialog.ShowMessage("确认要删除吗？", "提示", "", () => {
                        var count = stopTypeProvider.Delete(model);
                        if (count > 0)
                        {
                            dialog.ShowMessageBox("删除成功", "提示");
                            StopTypes = stopTypeProvider.Select();
                        }
                        else
                        {
                            dialog.ShowMessageBox("删除失败", "提示");
                        }
                    });
                    task.Start();
                });
            }
        }

    }
}
